package com.itheima.tlias84.service.impl;

import ch.qos.logback.core.testUtil.RandomUtil;
import com.alibaba.fastjson2.JSONObject;
import com.itheima.tlias84.entity.Emp;
import com.itheima.tlias84.mapper.EmpMapper;
import com.itheima.tlias84.po.LoginParam;
import com.itheima.tlias84.service.LoginService;
import com.itheima.tlias84.utils.JwtUtil;
import com.itheima.tlias84.utils.ResultUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class LoginServiceImpl implements LoginService {
    @Resource
    EmpMapper empMapper;
    @Override
    public ResultUtil login(LoginParam param) {
        //获取用户信息
        Emp emp = empMapper.selectEmpByUsername(param.getUsername());
        //判断用户信息
        if (Objects.isNull(emp)||!emp.getPassword().equals(param.getPassword())){
            return ResultUtil.fail("用户名或密码错误");
        }
        //获取token
        String token = JwtUtil.generateJwt(emp.getUsername(), emp.getPassword(), 30);
        //封装数据，返回给前端
        JSONObject result = new JSONObject();
        result.put("id",emp.getId());
        result.put("username",emp.getUsername());
        result.put("name",emp.getName());
        result.put("token",token);
        return ResultUtil.success(result);
    }
}
